package cn.saas.test;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import cn.saas.test.greendao.gen.DaoMaster;
import cn.saas.test.greendao.gen.DaoSession;

/**
 * @Title : 数据库帮助类
 * @date : 2021/5/19 14:56
 */
public class DbManager {
    private static DbManager mInstance;

    private DbManager() {
    }

    /**
     * 双重检测锁
     */
    public static DbManager getInstance() {
        if (mInstance == null) {
            synchronized (DbManager.class) {
                if (mInstance == null) {
                    mInstance = new DbManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化GreenDao，直接在Application中进行初始化操作
     */
    public void initGreenDao(Context context, String userId) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, userId + "_data.db");
        SQLiteDatabase db = helper.getWritableDatabase();//db读写数据库
        DaoMaster daoMaster = new DaoMaster(db);
        //创建daosession
        daoSession = daoMaster.newSession();
    }

    private DaoSession daoSession;

    public DaoSession getDaoSession() {
        return daoSession;
    }
}
